package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingShuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    //Request URL: http://localhost:8500/api/album/trackInfo/uploadTrack
    @PostMapping("/uploadTrack")
    @Operation(description = "上传声音")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPage/1/10
    @PostMapping("/findUserTrackPage/{pn}/{pz}")
    @Operation(description = "用户声音列表分页")
    @TingShuLogin
    public Result findUserTrackPage(@PathVariable Long pn,
                                    @PathVariable Long pz,
                                    @RequestBody TrackInfoQuery trackInfoQuery
    ) {
        IPage<TrackListVo> trackListVoPage = new Page<>();
        trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        trackListVoPage = trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        return Result.ok(trackListVoPage);
    }

    //Request URL: http://localhost:8500/api/album/trackInfo/saveTrackInfo
    @PostMapping("/saveTrackInfo")
    @Operation(description = "保存声音信息")
    @TingShuLogin
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    // Request URL: http://localhost:8500/api/album/trackInfo/getTrackInfo/51942
    @GetMapping("/getTrackInfo/{trackId}")
    @Operation(description = "获取声音信息")
    public Result getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    //Request URL: http://localhost:8500/api/album/trackInfo/updateTrackInfo/51942
    @PutMapping("/updateTrackInfo/{trackId}")
    @Operation(description = "更新声音信息")
    public Result updateTrackInfo(@PathVariable Long trackId,
                                  @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        return Result.ok();
    }
    //Request URL: http://localhost:8500/api/album/trackInfo/removeTrackInfo/51942
    @DeleteMapping("/removeTrackInfo/{trackId}")
    @Operation(description = "删除声音信息")
    public Result removeTrackInfo(@PathVariable Long trackId){
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }
    //Request URL: http://localhost:8500/api/album/trackInfo/findAlbumTrackPage/139/1/10
    @GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
    @Operation(description = "专辑声音列表分页")
    @TingShuLogin(required = false)
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Long pn,
                                     @PathVariable Long pz) {
        IPage<AlbumTrackListVo> albumTrackListVo = new Page<>(pn,pz);
        albumTrackListVo = trackInfoService.findAlbumTrackPage(albumTrackListVo, albumId);
        return Result.ok(albumTrackListVo);
    }
    //Request URL: http://localhost:8500/api/album/trackInfo/getTrackStatVo/51952
    @GetMapping("/getTrackStatVo/{trackId}")
    @Operation(description = "获取声音统计信息")
    public Result getTrackStatVo(@PathVariable(value = "trackId") Long trackId){
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }
    //Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPaidList/48243
    @GetMapping("/findUserTrackPaidList/{currentTrackId}")
    @Operation(description = "分页展示声音列表结算")
    @TingShuLogin
    public Result findUserTrackPaidList(@PathVariable(value = "currentTrackId") Long currentTrackId){
        List<Map<String,Object>> trackPaidMapList  = trackInfoService.findUserTrackPaidList(currentTrackId);
        return Result.ok(trackPaidMapList);
    }
}

